/*
 * p11300.cpp
 *
 *  Created on: 2013-4-19
 *      Author: zy
 */

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
long long a[1000005],C[1000005];
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		long long sum=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%lld",&a[i]);
			sum+=a[i];
		}
		long long M=sum/n;
		C[0]=0;
		for(int i=1;i<n;i++)C[i]=C[i-1]+a[i]-M;
		sort(C,C+n);
		long long x1 =C[n/2];
		long long ans=0;
		for(int i=0;i<n;i++)
			ans+=abs(x1-C[i]);
		cout<<ans<<endl;
	}
}
